# Author: williamjcm <w.jcm59@gmail.com>
# Contributor: mosra <mosra@centrum.cz>
# Based on the Corrade Arch Linux PKGBUILD maintained by xyproto and the MSYS2/MinGW PKGBUILD templates

_realname=corrade
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2020.06
pkgrel=3
pkgdesc='C++11/C++14 multiplatform utility library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='https://magnum.graphics/corrade/'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             'unzip')
source=("${_realname}-${pkgver}.zip::https://github.com/mosra/corrade/archive/v${pkgver}.zip"
        "fix-compilation-with-newer-libc++.patch")
noextract=("${_realname}-${pkgver}.zip")
sha256sums=('d89a06128c334920d91fecf23cc1df48fd6be26543dc0ed81b2f819a92d70e72'
            'c9ddc9c8acae34103e231e22b2372ec4d3c28d1072b7259531933a0b6cc49ff6')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}
  rm -rf ${_realname}-${pkgver}
  unzip ../${_realname}-${pkgver}.zip

  cd "${srcdir}/${_realname}-${pkgver}"
  # https://github.com/mosra/corrade/commit/a06a17c4d53ff8394eda0607fc6eb45526f2625d
  apply_patch_with_msg fix-compilation-with-newer-libc++.patch
}

build() {
    [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
    mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

    declare -a extra_config
    if check_option "debug" "n"; then
        extra_config+=("-DCMAKE_BUILD_TYPE=Release")
    else
        extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
    fi

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
        ${MINGW_PREFIX}/bin/cmake \
            -G'Ninja' \
            -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
            "${extra_config[@]}" \
            ../${_realname}-${pkgver}
    cmake --build .
}

package() {
    cd "${srcdir}"/build-${MSYSTEM}
    DESTDIR="${pkgdir}" cmake --install .

    install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" \
        "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
